package im.zhihao.zhdisk.module.user.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import im.zhihao.zhdisk.module.config.model.dto.SystemConfigDTO;
import im.zhihao.zhdisk.module.link.model.entity.ShortLink;
import im.zhihao.zhdisk.module.user.mapper.SystemUserMapper;
import im.zhihao.zhdisk.module.user.model.convert.SystemUserConvert;
import im.zhihao.zhdisk.module.user.model.dto.SystemUserDTO;
import im.zhihao.zhdisk.module.user.model.entity.SystemUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static im.zhihao.zhdisk.module.config.service.SystemConfigService.CACHE_NAME;

@Slf4j
@Service
public class SystemUserService {

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemUserConvert systemUserConvert;


    public SystemUser findById(Integer id) {
        return systemUserMapper.selectById(id);
    }

    public SystemUser findByUsername(String username) {
        QueryWrapper<SystemUser> queryWrapper = new QueryWrapper<>(new SystemUser())
                .eq(StrUtil.isNotEmpty(username), "username", username);
        return systemUserMapper.selectOne(queryWrapper);
    }

    public void removeById(Integer id) {
        log.info("删除 id 为 {} 的用户", id);
        systemUserMapper.deleteById(id);
    }

    public void updateSystemUser(SystemUserDTO systemUser){
        System.out.println(systemUser);
        UpdateWrapper<SystemUser> updateWrapper = new UpdateWrapper<>(new SystemUser())
                .set(StrUtil.isNotEmpty(systemUser.getPassword()), "password", systemUser.getPassword())
                .set(StrUtil.isNotEmpty(systemUser.getNickname()), "nickname", systemUser.getNickname())
                .eq("id", systemUser.getId());
        systemUserMapper.update(null, updateWrapper);
    }

    public Page<SystemUser> selectPage(Page<SystemUser> pages, QueryWrapper<SystemUser> queryWrapper) {
        return systemUserMapper.selectPage(pages, queryWrapper);
    }

    public void addSystemUser(SystemUserDTO systemUserDTO){
        SystemUser systemUser = systemUserConvert.DTOtoEntity(systemUserDTO);
//        systemUser.setCreateTime(new Date());
//        systemUser.setUpdateTime(new Date());
        systemUserMapper.insert(systemUser);
    }

    public void addUserStorage(Integer userId, Integer storageId){
        systemUserMapper.addUserStorage(userId, storageId);
    }

}
